//
//  HeartRateDayDataModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/12/7.
//

import UIKit

open class HeartRateChartDataModel: NSObject {
    
    /// 日期
    public var date: Date
    
    /// 是否正在测量
    public var isMeasure: Bool = false
    
    /// 平均数
    public var heartAvg: Int = 0
    
    /// 图表最大值
    public var chartMaxData: Int = 0
    
    /// 图表最小值
    public var chartMinData: Int = 0
    
    /// 图表Y轴线个数（不包含 0轴线）
    public var chartYAxisCount: Int = 4
    
    
    public var hasData = false
    
    public var chartDatas: [HeartBarData] = []
    
    open var heartRateTitle: String {
        
        return heartAvg.normalTitle
    }
    
    public init(date: Date) {
        self.date = date
        super.init()
        
        /// 获取三色图表数据
        let tuple = HeartRateChartViewProvider.heartChartBarData(date: date)
        
        self.heartAvg = tuple.avg
        self.chartDatas = tuple.barData ?? []
        
        let chartTuple = self.calculationYAxisValues(factsMax: tuple.max)
        
        // 获取到图表最大最小
        self.chartMaxData = chartTuple.YAxisMax
        self.chartMinData = chartTuple.YAxisMin
        self.chartYAxisCount = chartTuple.YAxisCount
        
        self.hasData = tuple.max > 0 && tuple.min < 255
        

    }
    
    public init(date: Date, heartRateModels: [HeartRateModel]) {
        self.date = date
        super.init()
        
        var dataSource: [HeartBarData] = []
        
        var all: [Int] = []
        var maxValue: Int = 0
        var minValue: Int = 0
        for heartModel in heartRateModels {
            
            let dataValue = HeartBarDataValue(high: Double(heartModel.max), low: Double(heartModel.min))
            let barData = HeartBarData(dayTime: dataValue, sleeping: .zero, workouts: .zero, startTime: 0, endTime: 0, avg: heartModel.avg)
            dataSource.append(barData)
            
            maxValue = Swift.max(heartModel.max, maxValue)
            if heartModel.min > 0 {
                minValue = Swift.min(heartModel.min, minValue)
            }
            
            
            let hearts = heartModel.hearts.filter({ $0 > 0 && $0 < 255 })
            all.append(contentsOf: hearts)
            
        }
        
        var avg = 0
        if !all.isEmpty {
            avg = all.reduce(0, { $0 + $1 })/all.count
        }
        
        heartAvg = avg
        chartDatas = dataSource
        
        let chartTuple = self.calculationYAxisValues(factsMax: maxValue)
        
        // 获取到图表最大最小
        self.chartMaxData = chartTuple.YAxisMax
        self.chartMinData = chartTuple.YAxisMin
        self.chartYAxisCount = chartTuple.YAxisCount
        
        self.hasData = avg > 0
        
    }
    
}

public extension HeartRateChartDataModel {
    
    /// 计算图表最大最小值设定
    /// - Parameter factsMax: 实际最大值
    /// - Returns: 元组（图表最大值，图表最小值， y轴线个数）
    func calculationYAxisValues(factsMax: Int) ->(YAxisMax: Int, YAxisMin: Int, YAxisCount: Int) {
        
        var max = factsMax
        let min = 0
        var count = 4
    
        if max < 190 {
            max = 200
        } else {
            let spcae = 100
            count = 4
            var addCount = 1
            let yu = max%spcae
            if spcae - yu < 10 {
                addCount = 2
            }
            max = max - yu + addCount * spcae
        }
        
        
        return (max, min, count)
        
    }
    
}
